﻿using SQLite;
using System.Collections.Generic;
using System.Threading.Tasks;

namespace StockTradTest.Data
{
    //账户操作
    public class MyAccountDatabase
    {
        readonly SQLiteAsyncConnection _database;

        public MyAccountDatabase(string dbPath)
        {
            _database = new SQLiteAsyncConnection(dbPath);
            _database.CreateTableAsync<MyAccount>().Wait();
        }
        public Task<MyAccount> GetAccountAsync(string accountName = "")
        {
            if(accountName == "")
            {
                return _database.Table<MyAccount>().FirstOrDefaultAsync();
            }
            else
            {
                return _database.Table<MyAccount>().Where(i => i.AccountName == accountName).FirstOrDefaultAsync();
            }

        }
        public Task<int> SaveAccountAsync(MyAccount myAccount)
        {
            if (myAccount == null) return Task.FromResult(0);
            MyAccount queryAccount = GetAccountAsync(myAccount.AccountName).Result;
            if (queryAccount!= null)
            {
                queryAccount.Fund = myAccount.Fund;
                return _database.UpdateAsync(queryAccount);
            }
            else
            {
                return _database.InsertAsync(myAccount);
            }
        }
        public Task<int> DeleteAccountAsync(MyAccount myAccount = null)
        {
            if (myAccount == null)
            {
                return _database.DeleteAllAsync<MyAccount>();
            }
            return _database.DeleteAsync(myAccount);
        }


    }
    //个人股票操作
    public class MyStockDatabase
    {
        readonly SQLiteAsyncConnection _database;

        public MyStockDatabase(string dbPath)
        {
            _database = new SQLiteAsyncConnection(dbPath);
            _database.CreateTableAsync<MyStock>().Wait();
        }
        public Task<List<MyStock>> GetAllStockAsync()
        {
            return _database.Table<MyStock>().ToListAsync();
        }
        public Task<MyStock> GetStockAsync(string stockCode)
        {

            return _database.Table<MyStock>()
                            .Where(i => i.StockCode == stockCode)
                            .FirstOrDefaultAsync();
        }
        public Task<int> SaveMyStockAsync(MyStock myStock)
        {

            MyStock queryStock = GetStockAsync(myStock.StockCode).Result;
            if (queryStock != null)
            {
                queryStock.StockNum = myStock.StockNum;
                return _database.UpdateAsync(queryStock);
            }
            else
            {
                return _database.InsertAsync(myStock);
            }
        }
        public Task<int> SaveMyStocksAsync(List<MyStock> myStock)
        {
            DeleteMyStockAsync();
            if(myStock!=null)return _database.InsertAsync(myStock);
            return Task.FromResult(0);
        }
        public Task<int> DeleteMyStockAsync(MyStock myStock = null)
        {
            if(myStock == null)
            {
                return _database.DeleteAllAsync<MyStock>();
            }
            MyStock queryStock = GetStockAsync(myStock.StockCode).Result;
            return _database.DeleteAsync(queryStock);
        }

    }
}
